package com.tanhua.server.controller;

import com.tanhua.model.enums.CommentType;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author げっしょく
 * @date 2021/10/30
 */
@RestController
@RequestMapping("/messages")
public class MessagesController {

    @Autowired
    private MessagesService messagesService;

    /**
     * 通过环信账号查找用户的信息
     * @param huanxinId
     * @return
     */
    @GetMapping("/userinfo")
    public ResponseEntity getUserInfo(String huanxinId) {
        UserInfoVo userInfoVo = messagesService.getUserInfo(huanxinId);
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 添加好友
     * @param map 发送好友请求的用户Id
     * @return
     */
    @PostMapping("/contacts")
    public ResponseEntity contacts(@RequestBody Map map) {
        Long userId = Long.valueOf(map.get("userId").toString());
        messagesService.addFriend(userId);
        return ResponseEntity.ok(null);
    }



    /**
     * 查询联系人列表
     * @param page
     * @param pagesize
     * @param keyword
     * @return
     */
    @GetMapping("/contacts")
    public ResponseEntity contacts(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   String keyword) {
        PageResult pageResult = messagesService.getFriends(page, pagesize, keyword);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询喜欢列表
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/loves")
    public ResponseEntity loves(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = messagesService.subject(page, pagesize, CommentType.LOVE);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询点赞列表
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/likes")
    public ResponseEntity likes(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = messagesService.subject(page, pagesize, CommentType.LIKE);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询评论列表
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = messagesService.subject(page, pagesize, CommentType.COMMENT);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 查询评论列表
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/announcements")
    public ResponseEntity announcements(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = messagesService.getNotice(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }
}
